package com.baibei.ebec.user.ticket;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.model.CouponInfo;
import com.baibei.model.RankingInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.ui.AppUI;
import com.baibei.widget.CommonRefreshLayout;
import com.rae.widget.RaePlaceHolderLayout;
import com.umeng.socialize.utils.Log;

import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 体验券
 * Created by ChenRui on 2017/8/21 0021 17:51.
 */
@Route(path = AppRouter.ROUTER_TICKET)
public class TicketActivity extends BasicActivity implements TicketContract.TicketView {

    @BindView(R2.id.rec_ticket)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout mRefreshLayout;
    @BindView(R2.id.placeholder)
    RaePlaceHolderLayout mPlaceholder;

    TicketAdapter mAdapter;

    private TicketContract.Presenter mPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket);
        mPlaceholder.dismiss();
        mAdapter = new TicketAdapter(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPresenter.start();
            }
        });

        mPresenter = inject(TicketContract.Presenter.class);
        mRefreshLayout.autoRefresh();
    }

    @Override
    public IPresenter getBasicPresenter() {
        return mPresenter;
    }

    @Override
    public void onLoadCouponList(List<CouponInfo> valid, List<CouponInfo> invalids) {
        android.util.Log.e("TAG","onLoadCouponList: "+invalids.toString() );
        mAdapter.setInfos(valid, invalids);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadCouponFailed(String msg) {
        AppUI.failed(this, msg);
    }

    @Override
    public void refreshCompleted() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.refreshComplete();
        }
    }

    @Override
    public void onLoadRankingInfoSuccess(List<RankingInfo> rankingInfoList) {
        mAdapter.setRankInfos(rankingInfoList);
    }
}
